package com.yixin.ystartlibrary.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Window;
import android.view.WindowManager;


import com.yixin.ystartlibrary.R;
import com.yixin.ystartlibrary.utils.DensityUtil;


/**
 * 旋转Dialog.
 */
public class RotationDialog extends AlertDialog {

    private RotationDialog(Context context) {
        super(context);
    }

    private RotationDialog(Context context, int theme) {
        super(context, theme);
    }

    protected RotationDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static AlertDialog show(Context context, CharSequence message,
                                   boolean cancelable, OnCancelListener cancelListener) {
        AlertDialog dialog = create(context, message, cancelable, cancelListener);
        dialog.show();
        return dialog;
    }

    public static AlertDialog create(Context context, CharSequence message,
                                     boolean cancelable, OnCancelListener cancelListener) {
        RotationDialog dialog = new RotationDialog(context);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        return dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.simple_item_dialog_rotation);
        setCanceledOnTouchOutside(false);

//        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
//        getWindow().setAttributes(params);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        if(window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = DensityUtil.dip2px(getContext(), 72);
            params.height = DensityUtil.dip2px(getContext(), 72);
            window.setAttributes(params);

            window.setBackgroundDrawableResource(R.drawable.drawable_loading_dialog_background);
            window.setDimAmount(0);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void setTitle(CharSequence title) {
    }

    @Override
    public void setMessage(CharSequence message) {}
}
